package com.example.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.app.HomePage;
import com.example.app.database.DBUserInfo;

import java.util.ArrayList;

public class LogInPage extends AppCompatActivity implements View.OnClickListener{

    private String realCode;
    private DBUserInfo mDBUserInfo;
    private TextView mTvLoginactivityRegister;
    private EditText mEtLoginactivityUsername;
    private EditText mEtLoginactivityPassword;
    private Button mBtLoginactivityLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        initView();

        mDBUserInfo = new DBUserInfo(this);
        mEtLoginactivityPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE)
                {
                    View view=(View)findViewById(R.id.login_in);
                    onClick(view);
                    return true;
                }
                return false;
            }
        });

    }

    private void initView() {
        // 初始化控件
        mBtLoginactivityLogin = findViewById(R.id.login_in);
        mTvLoginactivityRegister = findViewById(R.id.login_register);
        mEtLoginactivityUsername = findViewById(R.id.login_count);
        mEtLoginactivityPassword = findViewById(R.id.login_password);

        // 设置点击事件监听器
        mBtLoginactivityLogin.setOnClickListener(this);
        mTvLoginactivityRegister.setOnClickListener(this);

    }


    public void onClick(View view) {
        switch (view.getId()) {
            // 跳转到注册界面
            case R.id.login_register:
                startActivity(new Intent(this, RegisterPage.class));
                break;

            case R.id.login_in:
                String name = mEtLoginactivityUsername.getText().toString().trim();
                String password = mEtLoginactivityPassword.getText().toString().trim();

                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
                    ArrayList<User> data = mDBUserInfo.getAllData();
                    boolean match = false;
                    for (int i = 0; i < data.size(); i++) {
                        User user = data.get(i);
                        if (name.equals(user.getName()) && password.equals(user.getPassword())/*&&phoneCode.equals(realCode)*/) {
                            match = true;
                            break;
                        }
                        else {
                            match = false;
                        }
                    }

                        if (match) {
                            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(this, HomePage.class);
                            startActivity(intent);
                            //销毁此Activity
                        } else {
                            Toast.makeText(this, "用户名或密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                        }
                } else {
                    Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                }break;
        }
    }
}